<template>
  <!-- 招标议题需求请示 -->
  <CommonTitle titleName="招标议题需求请示"></CommonTitle>

  <div class="box">
    <el-form
      :inline="true"
      label-width="140"
      :model="state.formInline"
      :rules="rules"
      ref="formRef"
      class="demo-form-inline"
    >
      <el-row>
        <el-col :span="8" v-if="state.disabled">
          <el-form-item label="审核编号">
            <el-input
              v-model="state.formInline.flowId"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="state.disabled">
          <el-form-item label="审核状态">
            <el-select v-model="state.formInline.examineStatus" disabled>
              <el-option
                v-for="item in state.auditStatus"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>

            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="state.disabled">
          <el-form-item label=" ">
            <el-button
              type="primary"
              @click="openOa"
              class="button_edit"
              >查看流程审批</el-button
            >
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属部门">
            <el-input
              v-model="state.formInline.departmentName"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属项目集">
            <el-input
              v-model="state.formInline.projectSetName"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目集编号">
            <el-input
              v-model="state.formInline.projectSetCode"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目负责人">
            <el-input
              v-model="state.formInline.responsibleUserName"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属项目">
            <el-input
              v-model="state.formInline.projectName"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目编号">
            <el-input
              v-model="state.formInline.projectCode"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="议题性质">
            <el-input
              v-model="state.formInline.problemNature"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属阶段">
            <el-input
              v-model="state.formInline.projectStage"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目类型">
            <el-input
              v-model="state.formInline.projectType"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属任务">
            <el-input
              v-model="state.formInline.projectTask"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工作细项名称">
            <el-input
              v-model="state.formInline.itemName"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="任务编码">
            <el-input
              v-model="state.formInline.projectTaskCode"
              disabled
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="议题名称" prop="problemName">
            <el-input
              :disabled="state.disabled"
              v-model="state.formInline.problemName"
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <!--  预算控制价          -->
          <el-form-item label="控制价" prop="budgetControlPrice">
            <el-input
              :disabled="state.disabled"
              v-model="state.formInline.budgetControlPrice"
              placeholder="输入控制价"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="最高限价" prop="budgetPrice">
            <el-input
              :disabled="state.disabled"
              v-model="state.formInline.budgetPrice"
              placeholder="输入最高限价"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="招标方式" prop="projectTendersWay">
            <el-select
              :disabled="state.disabled"
              v-model="state.formInline.projectTendersWay"
              placeholder=""
            >
              <el-option
                v-for="item in projectTendersWayList"
                :key="item"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <!-- <el-col :span="8">
          <el-form-item label="紧急程度" prop="urgency">
            <el-select
              :disabled="state.disabled"
              v-model="state.formInline.urgency"
              placeholder=""
            >
              <el-option
                v-for="item in urgencyList"
                :key="item"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col> -->
        <!-- <el-col :span="8">
          <el-form-item label="是否人事议题" prop="personnelIssue">
            <el-select
              clearable
              :disabled="state.disabled"
              v-model="state.formInline.personnelIssue"
            >
              <el-option value="1" label="是"> </el-option>
              <el-option value="0" label="否"> </el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="服务期限" prop="startCheckTime">
            <el-date-picker
              :disabled="state.disabled"
              v-model="state.formInline.startCheckTime"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="资金来源" prop="fundSource">
            <el-input
              :disabled="state.disabled"
              v-model="state.formInline.fundSource"
              placeholder=""
            />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="招标类别" prop="biddingCategory">
            <el-select
              v-model="state.formInline.biddingCategory"
              :disabled="state.disabled"
              placeholder="招标类别"
              clearable
            >
              <el-option
                v-for="item in categoryOfTenderList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="任务来源" prop="taskSource">
            <el-input
              disabled
              v-model="state.formInline.taskSource"
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="24" >
          <el-form-item label="会审部门" prop="reviewDepartment">
            <el-select
              v-model="state.formInline.reviewDepartment"
              placeholder="会审部门"
              clearable
              :disabled="state.disabled"
              multiple
              filterable
            >
              <el-option
                v-for="item in state.departmentOptions"
                :key="item.departmentId"
                :label="item.departmentName"
                :value="item.departmentId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <!-- ////////////// -->
        <el-col :span="24">
          <el-form-item label=" " prop="serviceRange" class="select">
            <el-input
              :disabled="state.disabled"
              v-model="state.formInline.serviceRange"
              placeholder=""
            >
              <template #prepend>
                <el-select v-model="state.formInline.projectSummary" :disabled="state.disabled">
                  <el-option label="项目概括" value="1" />
                  <el-option label="实施范围" value="2" />
                  <el-option label="服务范围" value="3" />
                </el-select>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="议题简述" prop="projectOverview">
            <el-input
              :disabled="state.disabled"
              v-model="state.formInline.projectOverview"
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
        <!--            招标控制价说明 -->
          <el-form-item label="控制价/最高限价说明" prop="budgetControlPriceShow" label-width="200">
            <el-input
              :disabled="state.disabled"
              v-model="state.formInline.budgetControlPriceShow"
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="议题附件" prop="approvalFiles">
            <FileMinioUpload
              :disabled="state.disabled"
              :restriction="true"
              :restrictionLimit="9"
              @updateFiles="updateFiles"
              v-model:files="state.formInline.approvalFiles"
              :show-button="!state.disabled"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="律师意见" prop="files">
            <FileMinioUpload
              :disabled="state.disabled"
              :restriction="true"
              :restrictionLimit="9"
              @updateFiles="updateFiles"
              v-model:files="state.formInline.files"
              :show-button="!state.disabled"
            />
          </el-form-item>
        </el-col>

        <!-- 全部人员 -->
        <el-col :span="24">
          <CommonTitle titleName="其他信息"></CommonTitle>
        </el-col>
        <el-col :span="8">
          <el-form-item label="申请人">
            <el-input
              disabled
              v-model="state.formInline.createName"
              placeholder=""
            />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="申请部门">
            <el-input
              disabled
              v-model="state.formInline.createDepartmentName"
              placeholder=""
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="创建时间">
            <el-input
              disabled
              v-model="state.formInline.createTime"
              placeholder=""
            />
          </el-form-item>
        </el-col>

        <el-col :span="24" v-if="!state.disabled">
          <el-button @click="submit" type="primary" class="button_view">发起审批</el-button>
          <el-button type="primary" @click="goback" class="button_cancel2"> 取消 </el-button>
        </el-col>
      </el-row>
      <template #footer> </template>
    </el-form>
  </div>
</template>

<script setup>
import {ref, reactive, onMounted, onBeforeMount, watch, nextTick} from 'vue'
import {
  ElRow,
  ElCol,
  ElDatePicker,
  ElMessage,
  ElForm,
  ElButton,
  ElSelect,
  ElOption
} from 'element-plus'
import CommonTitle from '@/components/CommonTitle.vue'
import FileMinioUpload from "@/components/FileMinioUpload.vue";

import { biddingApi, department } from '@/api/project'
import { getURLhandler } from '@/views/constructionProjectManage/static/utools.js'
import { useRoute, useRouter } from 'vue-router'
import {useSetFormData} from "@/hooks/setFormData";
let router = useRouter()
let route = useRoute()
const state = reactive({
  auditStatus: [
    {
      label: '待审核',
      value: '1'
    },
    {
      label: '审核成功',
      value: '2'
    },
    {
      label: '审核驳回',
      value: '3'
    }
  ],
  user: '',
  projectTaskId: '',
  projectId: '',
  itemId: '',
  departmentOptions: [],

  formInline: {}
})

const {
    setData,
    getData,
    getAttributeNum,
    filterEmptyAttributes,
} = useSetFormData({
    key:`topic-${getURLhandler(route.path).projectId}-${getURLhandler(route.path).projectTaskId}-${getURLhandler(route.path).itemId}`,
});


const formRef = ref(null)
const rules = {
  problemName: [
    { required: true, message: '议题名称不能为空', trigger: 'blur' }
  ],
  biddingCategory: [
    { required: true, message: '招标类别不能为空', trigger: 'blur' }
  ],
  fundSource: [
    { required: true, message: '资金来源不能为空', trigger: 'blur' }
  ],
  projectTendersWay: [
    { required: true, message: '招标方式不能为空', trigger: 'blur' }
  ],
/*  budgetControlPrice: [
    { required: true, message: '预算控制价不能为空', trigger: 'blur' }
  ],*/
/*  budgetPrice: [
    { required: true, message: '预估金额不能为空', trigger: 'blur' }
  ],*/
  urgency: [{ required: true, message: '紧急程度不能为空', trigger: 'blur' }],
  personnelIssue: [
    { required: true, message: '人事议题不能为空', trigger: 'blur' }
  ],
  startCheckTime: [
    { required: true, message: '服务范围不能为空', trigger: 'blur' }
  ],
  projectOverview: [
    { required: true, message: '议题简述不能为空', trigger: 'blur' }
  ],
  serviceRange: [
    { required: true, message: '项目概况/实施范围/服务范围不能为空', trigger: 'blur' }
  ],
  budgetControlPriceShow: [
    { required: true, message: '控制价/最高限价说明不能为空', trigger: 'blur' }
  ],
  approvalFiles: [
    { required: true, message: '议题附件不能为空', trigger: 'blur' }
  ],
  reviewDepartment: [
    { required: true, message: '会审部门不能为空', trigger: 'blur' }
  ],
  // files: [{ required: true, message: '律师意见不能为空', trigger: 'blur' }]
}
//固定值 676628435627033400=工程类-施工类 -5707408146184845961=工程类-货物采购类 7560208448710396929=工程类-勘察、设计、监理服务类 -3267680363755058876=工程类-检测、监测、咨询、测绘、图审等其他工程服务类 413998337239041580=非工程类-修缮改造类 4291094611656655124=非工程类-设施设备系统维保及维修类 -1203932655659367791=非工程类-物业服务类 6837816167438960732=非工程类-物资采购类 -6752169256032911253=非工程类-其他服务类
const categoryOfTenderList = reactive([
  {
    label: '工程类-施工类',
    value: '3'
  },
  {
    label: '工程类-货物采购类',
    value: '4'
  },
  {
    label: '工程类-勘察、设计、监理服务类',
    value: '5'
  },
  {
    label: '工程类-检测、监测、咨询、测绘、图审等其他工程服务类',
    value: '6'
  },
  {
    label: '非工程类-修缮改造类',
    value: '7'
  },
  {
    label: '非工程类-设施设备系统维保及维修类',
    value: '8'
  },
  {
    label: '非工程类-物业服务类',
    value: '9'
  },
  {
    label: '非工程类-物资采购类',
    value: '10'
  },
  {
    label: '非工程类-其他服务类',
    value: '11'
  }
])
// 1, "公开招标
// 2, "公开竞争性谈判
// 3, "公开竞争性磋商
// 4, "择优比选"
// 5, "单一来源采购
// 6, "邀请竞争性谈判
// 7, "邀请竞争性磋商
// 8, "择优询价"
// 9, "单一来源采购
const projectTendersWayList = reactive([
  {
    label: '公开招标',
    value: '1'
  },
  {
    label: '公开竞争性谈判',
    value: '2'
  },
  {
    label: '公开竞争性磋商',
    value: '3'
  },
  {
    label: '择优比选',
    value: '4'
  },
  {
    label: '单一来源采购（公开）',
    value: '5'
  },
  {
    label: '邀请竞争性谈判',
    value: '6'
  },
  {
    label: '邀请竞争性磋商',
    value: '7'
  },
  {
    label: '择优询价',
    value: '8'
  },
  {
    label: '单一来源采购（非公开）',
    value: '9'
  }
])
//(0:普通,1:加急,2:平急,3:特急,4:特提)
const urgencyList = reactive([
  {
    label: '普通',
    value: '0'
  },
  {
    label: '加急',
    value: '1'
  },
  {
    label: '平急',
    value: '2'
  },
  {
    label: '特急',
    value: '3'
  },
  {
    label: '特提',
    value: '4'
  }
])

const handel = () => {}
const goback = () => {
  router.go(-1)
}
const submit = () => {
  formRef.value.validate(async (valid, fields) => {
    if (valid) {
      if (state.formInline.files.length == 0) {
        ElMessage.error('请上传律师意见')
        return
      }
      if (state.formInline.approvalFiles.length == 0) {
        ElMessage.error('请上传议题附件')
        return
      }
      state.formInline.projectId = state.projectId
      state.formInline.taskId = state.projectTaskId
      state.formInline.itemId = state.itemId
      let obj = JSON.parse(JSON.stringify(state.formInline))
      obj.startCheckTime = state.formInline.startCheckTime[0]
      obj.endCheckTime = state.formInline.startCheckTime[1]

      await biddingApi.biddingAdd(obj).then((res) => {
        if (res.code == '200') {
          ElMessage.success('操作成功')
          router.go(-1)
        }
      })
    }
  })
}

//获取所有部门信息
const getdepartmentList = () => {
  department.getAllDepartments().then((res) => {
    state.departmentOptions = res?.data || []
  })
}

const updateFiles = () => {}
onBeforeMount(() => {
  state.projectTaskId = getURLhandler(route.path).projectTaskId
  state.projectId = getURLhandler(route.path).projectId
  state.itemId = getURLhandler(route.path).itemId
  state.disabled = +getURLhandler(route.path).isLook
})
const openOa=()=>{
 window.open(state.formInline.routeUrl)
}

const isBool = ref(false);
watch([()=>state.disabled,()=>state.formInline],(newVal)=>{
   if(newVal[0]===0 &&  Object.keys(newVal[1]).length > 1 && isBool.value){
       setData(state.formInline);
   }
},{deep:true})

const getProjectData = () => {
    biddingApi
        .biddingLook({
            taskId: state.projectTaskId,
            itemId: state.itemId,
            projectId: state.projectId
        })
        .then(async (res) => {
            if (res.code === '200') {
                let obj = {};
                if (getAttributeNum(await getData()) > getAttributeNum(res.data)) { // 缓存中的数据比接口数据的多
                    obj = Object.assign({},await getData(), filterEmptyAttributes(res.data))
                }
                if (getAttributeNum(await getData()) <= getAttributeNum(res.data)) { // 缓存中的数据比接口数据的少或者相同
                    obj = filterEmptyAttributes(res.data)
                }

                for (const key in obj) {
                    switch (key) {
                        case 'startCheckTime':
                            if (state.disabled === 0) {
                                state.formInline[key] = obj[key] || [];
                            }
                            if (state.disabled === 1) {
                                state.formInline[key] = [
                                    obj[key] || '',
                                    obj['endCheckTime'] || ''
                                ];
                            }
                            break;
                        case 'files':
                            state.formInline[key] = obj[key] || [];
                            break;
                        case 'approvalFiles':
                            state.formInline[key] = obj[key] || [];
                            break;
                        case "projectSummary":
                            state.formInline[key] = obj[key] || '1';
                            break;
                        default:
                            state.formInline[key] = obj[key] || '';
                            break;
                    }
                }

                await nextTick(() => {
                    isBool.value = true;
                })
            }
        });
}

onMounted(() => {
    getProjectData();
    getdepartmentList();
})
</script>
<style lang="scss" scoped>
.el-form-item,
.el-select {
  width: 100%;
}
.box {
  border-radius: 10px;
  border: 1px solid #e3e7f1;
  padding: 34px 90px;
}
.select {
  .el-select {
    width: auto !important;
  }
}
</style>
